﻿Public Class ValidadorDeFormularios
    Private Frm As Form
    Private ErrorPrvdr As ErrorProvider
    Private ListaControles As ArrayList
    Public Sub New(pFrm As Form, pErrorPrvdr As ErrorProvider)
        ' Inicialiso el super
        MyBase.New()
        ' Asigno los valores
        Me.Frm = pFrm
        Me.ErrorPrvdr = pErrorPrvdr
        Me.ListaControles = New ArrayList
        Me.ObtenerListaControles(Me.Frm)
    End Sub

    Private Sub ObtenerListaControles(Cntrl As Control)
        ' Esta es la jeraquia que tienen los componentes que pueden contener otros componentes
        ' System.Windows.Forms.ScrollableControl
        '   System.Windows.Forms.SplitContainer
        '   System.Windows.Forms.FlowLayoutPanel
        '   System.Windows.Forms.Panel
        '       System.Windows.Forms.TabPage
        '       System.Windows.Forms.TableLayoutPane
        ' System.Windows.Forms.GroupBox
        ' System.Windows.Forms.TabControl

        For Each ctrl As Control In Cntrl.Controls
            If TypeOf ctrl Is ScrollableControl Or TypeOf ctrl Is GroupBox Or TypeOf ctrl Is TabControl Then
                Me.ObtenerListaControles(ctrl)
            ElseIf TypeOf ctrl Is TextBox Then
                Me.ListaControles.Add(ctrl)
            ElseIf TypeOf ctrl Is ComboBox Then
                Me.ListaControles.Add(ctrl)
            ElseIf TypeOf ctrl Is RichTextBox Then
                Me.ListaControles.Add(ctrl)
            End If
        Next

    End Sub

    ''' <summary>
    ''' Valida el formulario completo
    ''' Durate el proceso se encarga de mostrar los mensajes de error
    ''' </summary>
    ''' <returns>Si formulario paso la validacion</returns>
    ''' <remarks></remarks>
    Public Function ValidarFormulario() As Boolean
        Dim Valido As Boolean = True

        For Each ctrl As Control In Me.ListaControles
            Dim aux As Boolean = True

            If TypeOf ctrl Is TextBox Then
                aux = ValidarCampoTexto(ctrl)
            ElseIf TypeOf ctrl Is ComboBox Then
                aux = ValidarComboBox(ctrl)
            ElseIf TypeOf ctrl Is RichTextBox Then
                aux = ValidarRichTextBox(ctrl)
            End If

            If aux = False Then
                Valido = aux
            End If
        Next

        Return Valido
    End Function

    ''' <summary>
    ''' Valida un campo de texto
    ''' </summary>
    ''' <param name="TxtBx">El campo de texto a validar</param>
    ''' <returns>Si el campo de texto es valido</returns>
    ''' <remarks></remarks>
    Public Function ValidarCampoTexto(TxtBx As TextBox) As Boolean
        Dim Valido As Boolean = True
        Dim Nombre As String = TxtBx.Name


        If Nombre.StartsWith("txtString") Then

            If TxtBx.Text.Length = 0 Then
                ErrorPrvdr.SetError(TxtBx, "Campo requerido.")
                Valido = False
            Else
                ErrorPrvdr.SetError(TxtBx, "")
            End If

        ElseIf Nombre.StartsWith("txtInteger") Then

            Dim aux As Integer
            If Not Integer.TryParse(TxtBx.Text, aux) Then
                ErrorPrvdr.SetError(TxtBx, "Solo se aceptan numeros enteros.")
                Valido = False
            Else
                ErrorPrvdr.SetError(TxtBx, "")
            End If

        ElseIf Nombre.StartsWith("txtDecimal") Then

            Dim aux As Decimal
            If Not Decimal.TryParse(TxtBx.Text, aux) Then
                ErrorPrvdr.SetError(TxtBx, "Solo se aceptan numeros con decimales.")
                Valido = False
            Else
                ErrorPrvdr.SetError(TxtBx, "")
            End If
        End If

        Return Valido
    End Function

    ''' <summary>
    ''' Valida un combo box
    ''' </summary>
    ''' <param name="CmbBx">El combobox a validar</param>
    ''' <returns>Si el combobox es valido</returns>
    ''' <remarks></remarks>
    Public Function ValidarComboBox(CmbBx As ComboBox) As Boolean
        Dim Valido As Boolean = True

        If CmbBx.Name.StartsWith("cmbRequerido") Then
            If CmbBx.SelectedItem Is Nothing Then
                ErrorPrvdr.SetError(CmbBx, "Selecione una opcion.")
                Valido = False
            Else
                ErrorPrvdr.SetError(CmbBx, String.Empty)
            End If
        End If

        Return Valido
    End Function

    ''' <summary>
    ''' Valida un campo de texto enriquesido
    ''' </summary>
    ''' <param name="RchTxtBx">El control a validar</param>
    ''' <returns>Si el control tiene contenido</returns>
    ''' <remarks></remarks>
    Public Function ValidarRichTextBox(RchTxtBx As RichTextBox) As Boolean
        Dim Valido As Boolean = True

        If RchTxtBx.Name.StartsWith("rtbString") Then
            If RchTxtBx.Text = String.Empty Then
                ErrorPrvdr.SetError(RchTxtBx, "Campo requerido.")
                Valido = False
            Else
                ErrorPrvdr.SetError(RchTxtBx, String.Empty)
            End If
        End If

        Return Valido
    End Function

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class
